﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Linq;
using System.Text;
using Dataglot;

namespace IrishSettings
{
    public class InsertSettingsCommand:GenericCommand
    {
        public InsertSettingsCommand(DataFactory factory,DbConnection shared, DbTransaction transaction)
            : base(factory,shared,transaction)
        {
            ConfigureCommand();
        }

        public InsertSettingsCommand(DataFactory factory):base(factory)
        {
            ConfigureCommand();
        }

        private void ConfigureCommand()
        {
            const string sql = @"INSERT INTO [{$ns}_settings_values]
           (serialized_value)
     VALUES
           (@serialized_value)
";
            ComposeSql(sql);
            
            AddParameter("@serialized_value", DbType.String);
            //AddParameter("@name", DbType.String);
            //AddParameter("@version", DbType.String);
            //AddParameter("@scope", DbType.String);
            //AddParameter("@application", DbType.String);
            //AddParameter("@value_type", DbType.String);
            //AddParameter("@culture", DbType.String);
            //AddParameter("@user_id", DbType.Int32);
        }

        public void InsertApplication(
            string name,
            string serializedValue,
            Type valueType,
            string application,
            Version version,
            CultureInfo culture
            )
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("Missing setting name", "name");
            if (string.IsNullOrEmpty(application))
                throw new ArgumentException("Missing application name", "application");
            if (version == null)
                throw new ArgumentException("Missing version", "version");
            if (string.IsNullOrEmpty(serializedValue))
            {
                throw new ArgumentException("Serialized Value is missing for " +name);
            }
            if (valueType==null)
            {
                throw new ArgumentException("Value type is missing for " + name);
            }
            if (culture== null)
            {
                throw new ArgumentException("culture info is missing for " + name);
            }
            //command.Parameters["@name"].Value = name;
            command.Parameters["@serialized_value"].Value = serializedValue;
            //command.Parameters["@version"].Value = version.ToString();
            //command.Parameters["@scope"].Value = "Application"; //Case matters
            //command.Parameters["@application"].Value = application;
            //command.Parameters["@value_type"].Value = valueType.ToString();
            //command.Parameters["@user_id"].Value = userId;
            //command.Parameters["@culture"].Value = culture.Name;//e.g. en-US
            int rows=ExecuteNonQuery();
            if(rows==0)
            {
                throw new InvalidOperationException("Expected 1 row to be affected, but it was 0");
            }
        }
    }
}
